﻿SmitearWebGL.Core.Math.Vector3 = Class.extend(
{
	/******** constructor ********/
	
	init : function( x, y, z )
	{
		this.x = x == undefined ? 0 : x;
		this.y = y == undefined ? 0 : y;
		this.z = z == undefined ? 0 : z;
		
		this._registClass( "SmitearWebGL.Core.Math.Vector3" );
	}
	,
	reset : function( x, y, z )
	{
		this.x = x == undefined ? 0 : x;
		this.y = y == undefined ? 0 : y;
		this.z = z == undefined ? 0 : z;
	}
	,
	zero : function()
	{
		this.x = this.y = this.z = 0.0;
	}
	,
	clone : function()
	{
		return $V3( this.x, this.y, this.z );
	}
	,
	copy : function( v )
	{
		this.x = v.x;
		this.y = v.y;
		this.z = v.z;
	}
	,
	multiply : function( v )
	{
		this.x *= v.x;
		this.y *= v.y;
		this.z *= v.z;
	}
	,
	add : function( v )
	{
		this.x += v.x;
		this.y += v.y;
		this.z += v.z;
	}
	,
	subtract : function( v )
	{
		this.x -= v.x;
		this.y -= v.y;
		this.z -= v.z;
	}
	,
	opposite : function()
	{
		this.x = - this.x;
		this.y = - this.y;
		this.z = - this.z;
	}
	,
	normalize : function()
	{
		var mod = this.modulo();

		if( mod != 0 && mod != 1 )
		{
			mod = 1 / mod;
			
			this.x *= mod;
			this.y *= mod;
			this.z *= mod;
		}
	}
	,
	modulo : function()
	{
		return Math.sqrt( this.moduloSquared() );
	}
	,
	moduloSquared : function()
	{
		return ( this.x * this.x + this.y * this.y + this.z * this.z );
	}
	,
	dot : function( v )
	{
		return ( this.x * v.x + this.y * v.y + this.z * v.z );
	}
	,
	cross : function( v )
	{
		this.reset( ( this.y * v.z ) - ( this.z * v.y ), ( this.z * v.x ) - ( this.x * v.z ), ( this.x * v.y ) - ( this.y * v.x ) );
	}
	,
	//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * trace * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	toString : function()
	{
		return "SmitearWebGL.Core.Math.Vector3(" + this.x + ", " + this.y + ", " + this.z + ")";
	}
	,
	flatten : function ()
	{
		return [this.x, this.y, this.z];
	}
	,
	isEqualTo : function( v )
	{
		return ( this.x == v.x && this.y == v.y && this.z == v.z );
	}
}
);

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// 												 * * * * * STATIC * * * * *
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * CREATE * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

SmitearWebGL.Core.Math.Vector3.create = function( e0, e1, e2 )
{
	return new SmitearWebGL.Core.Math.Vector3( e0, e1, e2 );
}

SmitearWebGL.Core.Math.Vector3.ZERO = function()
{
	return this.create( 0, 0, 0 );
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * OPERATIONS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

SmitearWebGL.Core.Math.Vector3.normal = function( v )
{
	var vn = v.clone();
	vn.normalize();

	return vn;
}

SmitearWebGL.Core.Math.Vector3.add = function( v, w )
{
	return this.create( v.x + w.x, v.y + w.y, v.z + w.z );
}

SmitearWebGL.Core.Math.Vector3.sub = function( v, w )
{
	return this.create( v.x - w.x, v.y - w.y, v.z - w.z );
}

SmitearWebGL.Core.Math.Vector3.cross = function( v, w, targetN )
{
	if ( targetN == undefined )
	targetN = this.ZERO();

	targetN.reset( ( v.y * w.z ) - ( v.z * w.y ),
	( v.z * w.x ) - ( v.x * w.z ),
	( v.x * w.y ) - ( v.y * w.x ) );
	
	return targetN;
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

var $V3 = SmitearWebGL.Core.Math.Vector3.create;
